package com.mapper;

import com.pojo.NoteProducts;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NoteProductsMapper {

    @Insert("INSERT INTO note_products(note_id, product_id, created_at) " +
            "VALUES(#{noteId}, #{productId}, #{createdAt})")
    int insert(NoteProducts noteProduct);

    @Delete("DELETE FROM note_products WHERE note_id = #{noteId} AND product_id = #{productId}")
    int delete(@Param("noteId") Integer noteId, @Param("productId") Integer productId);

    @Delete("DELETE FROM note_products WHERE note_id = #{noteId}")
    int deleteByNoteId(Integer noteId);

    @Delete("DELETE FROM note_products WHERE product_id = #{productId}")
    int deleteByProductId(Integer productId);

    @Select("SELECT * FROM note_products WHERE note_id = #{noteId}")
    List<NoteProducts> selectByNoteId(Integer noteId);

    @Select("SELECT * FROM note_products WHERE product_id = #{productId}")
    List<NoteProducts> selectByProductId(Integer productId);

    @Select("SELECT COUNT(*) FROM note_products WHERE note_id = #{noteId} AND product_id = #{productId}")
    int exists(@Param("noteId") Integer noteId, @Param("productId") Integer productId);

    @Select("SELECT product_id FROM note_products WHERE note_id = #{noteId}")
    List<Integer> selectProductIdsByNoteId(Integer noteId);

    @Select("SELECT note_id FROM note_products WHERE product_id = #{productId}")
    List<Integer> selectNoteIdsByProductId(Integer productId);
}